jQuery(function($){
  // 点击显示审核框子
    $("a.js-to-check-this-salary").on("click", function(e){
      var checkSalaryTemp = JST["ems/store_staff_salaries/check_salary"]
      e.preventDefault();
      var staffId = $(this).parents("tr.js-salary-item-tr").data("id");
      $.ajax({
        url: "/api/store_staffs/"+staffId,
        success: function(data){
          if(data.status){
            $("div.js-show-check-salary-area").html(checkSalaryTemp(data))
          }else {
            showErrorView("服务器出错!");
          }
        }
      })
    });

    //点击保存更新内容，目前已经移除
    $(document).on("click", "input.js-to-update-salary", function(e){
      e.preventDefault();
      var id = $(this).parents("form#update_salary").data("id");
      var formData = $(this).closest("form#update_salary").serializeJSON();
      // var csrf = $("input#js-ems-store-salaries-update").val();
      formData.store_staff_salary.status = false
      updateSalary(formData, id)
    });

    //js-to-check-salary 点击审核去改变状态为已经审核
    $(document).on("click", "input.js-to-check-salary", function(e){
      e.preventDefault();
      var id = $(this).parents("form#update_salary").data("id");
      var formData = $(this).closest("form#update_salary").serializeJSON();
      // var csrf = $("input#js-ems-store-salaries-update").val();
      formData.store_staff_salary.status = true
      updateSalary(formData, id)
    })

    // 这个是ajax请求
    function updateSalary(data, id){
      var csrf = $("input#js-ems-store-salaries-update").val();
      $.ajax({
        url: "/api/store_staff_salaries/"+id,
        method: "put",
        data: data,
        headers:{
          'X-CSRF-Token': csrf
        },
        success: function(data){
          if(data.status){
            $("div.js-show-check-salary-area").html("");
            location.href = "/ems/store_staff_salaries"
          }else {
            showErrorView("保存失败，服务器出错！")
          }
        }
      })
    };
    
})
